<template>
	<view>
		<view class="box">
			<view class="boxItem">
				<input type="text" class="gui-form-input"
					v-model="newData" @input="inputting" placeholder="请输入内容" />
					<text class="gui-form-icon gui-icons gui-text-center gui-color-gray"
									v-if="newData.length >= 1" @click="clearName2">&#xe632;</text>
			</view>
			<view class="saveBtn" @tap="submit">保存</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				oldData: '',
				newData: '',
				type:1,
				typeName:''
			}
		},
		onLoad(options) {
			console.log(options);
			this.type= parseInt(options.type)
			this.typeName=this.type==1?'昵称':this.type==2?'真实姓名':this.type==3?'客户号':this.type==4?'邮箱':'微信号'
			uni.setNavigationBarTitle({
			　　title:'修改'+this.typeName
			})
			if(options.data&&options.data!='null'){
				this.newData = options.data
			}
			this.oldData = options.data
		},
		computed:{
			...mapState(['userInfo'])
		},
		methods: {
			...mapMutations(['login']),
			// 监听输入框输入事件
			inputting : function (e) {
					this.newData = e.detail.value;
			},
					// 清空输入框的值
			clearName2 : function () {
					this.newData = '';
			},
			submit(){
				let formData={}
				if (!this.newData) {
					this.$u.msg('请输入'+this.typeName)
					return;
				}
				if (this.newData == this.oldData) {
					this.$u.msg(this.typeName+'未更换哦')
					return;
				}
				if(this.type==1){
					formData.nickname=this.newData
				}else if(this.type==2){
					formData.realName=this.newData
				}else if(this.type==3){
					formData.customerNum=this.newData
				}else if(this.type==4){
					formData.email=this.newData
				}else if(this.type==5){
					formData.wechat=this.newData
				}
				this.$http.post('/api/worker/update',formData).then(res => {
					console.log(res)
					if (res.data.code == 200) {
						this.$u.msg('修改成功！')
						if(this.type==1){
							this.userInfo.nickname=formData.nickname
						}else if(this.type==2){
							this.userInfo.realName=formData.realName
						}else if(this.type==3){
							this.userInfo.customerNum=formData.customerNum
						}else if(this.type==4){
							this.userInfo.email=formData.email
						}else if(this.type==5){
							this.userInfo.wechat=formData.wechat
						}
						this.login(this.userInfo)
						this.$u.back(1,1000)
					} else {
						this.$u.msg(res.data.msg)
					}
				})
			}
		}
	}
</script>

<style lang="less">
page{background: #F6F7F8;}
.box{
	padding-top: 8rpx;
	.boxItem{display: flex;justify-content: space-between;align-items: center;background: #FFFFFF;padding: 30rpx;}
	}
	.saveBtn{
		height: 80rpx;
		font-size: 32rpx;
		font-weight: 700;
		margin: 300rpx 50rpx 100rpx 50rpx;
	}
</style>
